package com.dongdongshop.service.impl;

import cn.hutool.json.JSONObject;
import com.dongdongshop.mapper.TbSeckillGoodsMapper;
import com.dongdongshop.mapper.TbSeckillOrderMapper;
import com.dongdongshop.pojo.TbSeckillGoods;
import com.dongdongshop.pojo.TbSeckillGoodsExample;
import com.dongdongshop.pojo.TbSeckillOrder;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.SeckillService;
import com.dongdongshop.util.IdWorker;
import com.google.gson.JsonObject;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;


@Service
@DubboService
public class SeckillServiceImpl implements SeckillService {
    @Autowired
    private TbSeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private TbSeckillOrderMapper seckillOrderMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<TbSeckillGoods> getSeckills() {
        //将查询出的秒杀商品放入redis
        //将库存单独存进redis 需要做判断时单取库存即可
        TbSeckillGoodsExample example = new TbSeckillGoodsExample();
        TbSeckillGoodsExample.Criteria criteria = example.createCriteria();
        Date date = new Date();
        criteria.andStatusEqualTo("1");
        criteria.andStockCountGreaterThan(0);
        criteria.andStartTimeLessThanOrEqualTo(date);
        criteria.andEndTimeGreaterThanOrEqualTo(date);
        List<TbSeckillGoods> tbSeckillGoods = seckillGoodsMapper.selectByExample(example);
        for (TbSeckillGoods seck : tbSeckillGoods) {
            redisTemplate.boundHashOps("seckill").put(seck.getId(),seck);
            stringRedisTemplate.opsForValue().set(String.valueOf(seck.getId()),String.valueOf(seck.getStockCount()));
        }
        return null;
    }

    @Override
    public List<TbSeckillGoods> getSeckillList() {
        //得到redis中的所有商品
        List<TbSeckillGoods> list = redisTemplate.boundHashOps("seckill").values();
        return list;
    }

    @Override
    public TbSeckillGoods getSeckillById(Long id) {
        //查询redis中的单个
        return (TbSeckillGoods) redisTemplate.boundHashOps("seckill").get(id);
    }

    @Override
    public void addSeckill(Long id, TbUser user) throws Exception {
        //redis取出需要的秒杀商品
        TbSeckillGoods seckill = getSeckillById(id);
        //取出库存判断
        String s = stringRedisTemplate.opsForValue().get(String.valueOf(id));
        if(Long.valueOf(s) <= 0){
           throw new Exception("卖完了");
        }
        //自减一后返回剩余
        Long decrement = stringRedisTemplate.opsForValue().decrement(String.valueOf(id));
        //剩余数量 set进原有的剩余
        seckill.setStockCount(Math.toIntExact(decrement));
        //修改覆盖
        redisTemplate.boundHashOps("seckill").put(seckill.getId(),seckill);
        //如果等于0 修改mysql
        if (decrement == 0){
            seckillGoodsMapper.updateByPrimaryKeySelective(seckill);
        }
        //mq调用存入订单
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOpt("goodsId",id);
        jsonObject.putOpt("username",user.getUsername());
        rocketMQTemplate.convertAndSend("create-order-topic",jsonObject.toString());

    }

    @Override
    public Long selectRedisOrder(String username) {
        Long s = (Long) redisTemplate.boundValueOps("user_" + username).get();
        return s;
    }
}
